package com.eascs.web.o2o.web.aging.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.web.o2o.web.aging.service.SysOrgDao;
import com.eascs.web.o2o.web.aging.service.SysOrgService;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;


@Service
public class SysOrgServiceImpl implements SysOrgService {
	@Autowired
	private SysOrgDao sysOrgDao;

	

	@Override
	public List<ActOrgEntity> getOrgProvince() {
		return sysOrgDao.getOrgProvince();
	}
	
/*	@Override
	public List<ActOrgEntity> getOrgListByLevel(String parentId, int level) {
		return sysOrgDao.getOrgListByLevel(parentId, level);
	}

	@Override
	public ModelResult<List<Object[]>> getOrgListLikeName(String orgName, Integer level, User loginUser) {
		// set login User for data permission
		UserHolder.setUser(loginUser);
		ModelResult<List<Object[]>> modelResult = new ModelResult<>();
		try {
			if (null != level && level > 0) {
				modelResult.withModel(sysOrgDao.getOrgListLikeName(orgName, level));
			} else {
				modelResult.withModel(sysOrgDao.getOrgAllLikeName(orgName));
			}
		} catch (Exception e) {
			logger.error("组织架构模糊搜索失败", e);
			modelResult.withError("SysPermissionServiceImpl.getOrgLikeName", "组织架构模糊搜索失败:" + e.getMessage());
		}
		return modelResult;
	}

	@Override
	public ModelResult<List<Object[]>> getOrgListLikeNameWithLevels(String orgName, List<Integer> levelList,
	                                                                User loginUser) {
		// set login User for data permission
		UserHolder.setUser(loginUser);
		ModelResult<List<Object[]>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysOrgDao.getOrgListLikeNameWithLevels(orgName, levelList));
		} catch (Exception e) {
			logger.error("组织架构模糊搜索失败", e);
			modelResult.withError("SysPermissionServiceImpl.getOrgLikeName", "组织架构模糊搜索失败:" + e.getMessage());
		}
		return modelResult;
	}

	@Override
	public ModelResult<List<Object[]>> get380Employee(String name) {
		ModelResult<List<Object[]>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysOrgDao.get380EmployeeLikeName(name));
		} catch (Exception e) {
			logger.error("清收人联想接失败", e);
			modelResult.withError("SysPermissionServiceImpl.get380EmployeeLikeName", "清收人联想接失败:" + e.getMessage());
		}
		return modelResult;
	}

	@Override
	public List<Object[]> getOrgAllLikeName(String orgName) {
		return sysOrgDao.getOrgAllLikeName(orgName);
	}

	@Override
	public List<ActOrgEntity> getOrgAllList() {
		return sysOrgDao.getOrgAllList();
	}

	@Override
	public ActOrgEntity getOrgByIds(String ids) {
		return sysOrgDao.getOrgByIds(ids);
	}

	@Override
	public ActOrgEntity insert(ActOrgEntity orgEntity) {
		sysOrgDao.insert(orgEntity);
		return orgEntity;
	}

	@Override
	public int update(ActOrgEntity orgEntity) {
		if (orgEntity.getOrgId().equals(orgEntity.getParentId())) {
			return 0;
		}
		return sysOrgDao.update(orgEntity);
	}

	@Override
	public int delete(String ids) {
		return sysOrgDao.delete(ids);
	}

	@Override
	public ActOrgDetail getOrgDetailByUnitCode(String unitCode) {
		return sysOrgDao.getOrgDetailByUnitCode(unitCode);
	}

	@Override
	public ActOrgDetail getOrgDetailByUnitNo(String unitNo) {
		return sysOrgDao.getOrgDetailByUnitNo(unitNo);
	}

	@Override
	public ActOrgEntity getOrgByUnitNo(String unitNo) {
		return sysOrgDao.getOrgUnitNo(unitNo);
	}

	@Override
	public List<ActOrgEntity> getChildOrgByUnitNo(String unitNo) {
		List<ActOrgEntity> result = new ArrayList<ActOrgEntity>();
		ActOrgEntity orgEntity = sysOrgDao.getOrgUnitNo(unitNo);
		if (orgEntity != null) {
			// 获取所有的子节点
			result.add(orgEntity);
			List<ActOrgEntity> tmpActOrgList = new ArrayList<ActOrgEntity>();
			tmpActOrgList.add(orgEntity);
			while (tmpActOrgList != null && !tmpActOrgList.isEmpty()) {
				List<String> parentIds = getParentIds(tmpActOrgList);
				tmpActOrgList = sysOrgDao.getOrgByParentIds(parentIds);
				if (tmpActOrgList != null && !tmpActOrgList.isEmpty()) {
					result.addAll(tmpActOrgList);
				}
			}

			// 移除非平台的节点
			Iterator<ActOrgEntity> iterators = result.iterator();
			while (iterators.hasNext()) {
				if (iterators.next().getOrgLevel() != ActOrgEntity.ORG_LEVEL_PLATFORM) {
					iterators.remove();
				}
			}
		}
		return result;
	}

	private List<String> getParentIds(List<ActOrgEntity> actOrgList) {
		List<String> parentIds = new ArrayList<String>();
		for (ActOrgEntity org : actOrgList) {
			parentIds.add(org.getOrgId());
		}
		return parentIds;
	}

	@Override
	public ModelResult<PageResult<OrgUserVO>> queryPageActOrgUser(String orgId, int pageSize, int pageIndex) {
		return sysPermissionService.queryPageActOrgUser(orgId, pageSize, pageIndex);
	}

	@Override
	public ModelResult<Integer> insertActOrgUser(ActOrgUser actOrgUser) {
		return sysPermissionService.insertActOrgUser(actOrgUser);
	}

	@Override
	public ModelResult<Integer> deleteActOrgUser(String id, String userNo) {
		return sysPermissionService.deleteActOrgUser(id, userNo);
	}

	@Override
	public ModelResult<ActOrgUser> getOrgUserByUserNo(String userNo) {
		ModelResult<ActOrgUser> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysOrgDao.getOrgUserByUserNo(userNo));
		} catch (Exception e) {
			logger.error("查询用户组织关系失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.getOrgUserByUserNo", "查询用户组织关系失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public ModelResult<ActOrgEntity> getActOrgByUserNo(String userNo) {
		ModelResult<ActOrgEntity> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysOrgDao.getActOrgByUserNo(userNo));
		} catch (Exception e) {
			logger.error("查询用户组织机构失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.getActOrgByUserNo", "查询用户组织机构失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public ModelResult<List<ActOrgEntity>> getAllOrgParentList(String orgId) {
		ModelResult<List<ActOrgEntity>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysOrgDao.getAllOrgParentList(orgId, null));
		} catch (Exception e) {
			logger.error("查询父级组织机构树失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.getAllOrgParentList", "查询父级组织机构树失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public List<ActOrgEntity> getChildOrgByOrgId(String orgId) {
		ActOrgEntity orgEntity = sysOrgDao.getOrgByIds(orgId);
		return getChildOrg(orgEntity);
	}

	@Override
	public List<ActOrgEntity> getChildOrgByName(String name) {
		ActOrgEntity orgEntity = sysOrgDao.getOrgByName(name);
		return getChildOrg(orgEntity);
	}

	@Override
	public ModelResult<List<ActOrgEntity>> getOrgListByRoleAndUser(String userNo) {
		ModelResult<List<ActOrgEntity>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysOrgDao.getOrgListByRoleAndUser(userNo));
		} catch (Exception e) {
			logger.error("查询用户组织机构失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.getActOrgByUserNo", "查询用户组织机构失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	private List<ActOrgEntity> getChildOrg(ActOrgEntity orgEntity) {
		List<ActOrgEntity> result = new ArrayList<ActOrgEntity>();
		if (orgEntity != null) {
			// 获取所有的子节点
			result.add(orgEntity);
			List<ActOrgEntity> tmpActOrgList = new ArrayList<ActOrgEntity>();
			tmpActOrgList.add(orgEntity);
			while (tmpActOrgList != null && !tmpActOrgList.isEmpty()) {
				List<String> parentIds = getParentIds(tmpActOrgList);
				tmpActOrgList = sysOrgDao.getOrgByParentIds(parentIds);
				if (tmpActOrgList != null && !tmpActOrgList.isEmpty()) {
					result.addAll(tmpActOrgList);
				}
			}

			// 移除非平台的节点
			Iterator<ActOrgEntity> iterators = result.iterator();
			while (iterators.hasNext()) {
				Integer level = iterators.next().getOrgLevel();
				if (level != ActOrgEntity.ORG_LEVEL_PLATFORM && level != ActOrgEntity.ORG_LEVEL_PROVINCE_FIN_COM) {
					iterators.remove();
				}
			}
		}
		return result;
	}*/

	

	/*@Override
	public List<ActOrgEntity> getOrgChildrenByOrgId(String orgId) {
		if (StringUtils.isBlank(orgId)) {
			return null;
		}

		return sysOrgDao.getOrgChildrenByOrgId(orgId);
	}

	@Override
	public ActOrgEntity getProvincialByOrgUnito(String unito) {
		return sysOrgDao.getProvincialByOrgUnito(unito);
	}

	@Override
	public ModelResult<List<PlatformRegional>> getPlatformRegionalList() {
		ModelResult<List<PlatformRegional>> modelResult = new ModelResult<List<PlatformRegional>>();
		List<PlatformRegional> resultList = orgInletService.getPlatformRegionalList();
		modelResult.setModel(resultList);
		return modelResult;
	}

	@Override
	public ModelResult<List<ActOrgEntity>> getProvinceWithUser(String userNo) {
		return orgInletService.getProvinceWithUser(userNo);
	}

	@Override
	public ModelResult<List<SysUser>> getCreditMGRWithUser(String userNo) {
		return orgInletService.getCreditMGRWithUser(userNo);
	}

	@Override
	public ModelResult<List<ActOrgEntity>> getPlatformWithUser(String userNo) {
		return orgInletService.getPlatformWithUser(userNo);
	}

	@Override
	public List<ActOrgEntity> getOrgByParentIdAndLevel(String parentId, int level) {
		return sysOrgDao.getOrgByParentIdAndLevel(parentId, level);
	}

	@Override
	public List<ActOrgEntity> getLeafNodeByParentId(String orgId) {
		return sysOrgDao.getLeafNodeByParentId(orgId);
	}

	@Override
	public String getProvinceLetterCode(String provinceCode) {
		return sysOrgDao.getProvinceLetterCode(provinceCode);
	}

	@Override
	public ModelResult<List<SysUser>> getCreditMGRAndMarketerWithUser(String userNo) {
		return orgInletService.getCreditMGRAndMarketerWithUser(userNo);
	}

	@Override
	public List<User> checkDataPermission(String userNo) {
		// 先检查是否是区域经理
		List<HREmployeeAuditorEntity> empList = hREmplAuditorDao.getEmployeeByAuditorNo(userNo, "RM");
		// 不为空，则是区域经理
		if (!CollectionUtils.isEmpty(empList)) {
			// 判断是否还有其他的省区据色色
			List<String> roleList = new ArrayList<String>();
			roleList.add("bldj");
			roleList.add("1008");
			roleList.add("O2O-SHJRZJ");
			Boolean hasOtherRole = sysUserDao.checkUserRole(userNo, roleList);
			if (hasOtherRole) {
				return null;
			}

			Set<String> allUserNoList = new HashSet<String>();
			Set<String> userNoList = new HashSet<String>();
			userNoList.add(userNo);
			for (HREmployeeAuditorEntity e : empList) {
				userNoList.add(e.getEmployeeNo());
			}
			allUserNoList.addAll(userNoList);
			// 查找业务交接人
			for (String cm : userNoList) {
				List<HREmployeeAuditorEntity> empBoList = hREmplAuditorDao.getEmployeeByAuditorNo(cm, "BO");
				if (!CollectionUtils.isEmpty(empBoList)) {
					for (HREmployeeAuditorEntity bo : empBoList) {
						allUserNoList.add(bo.getEmployeeNo());
					}
				}
			}
			return sysUserDao.getUserByNosWithDMS(new ArrayList<String>(allUserNoList));
		}

		// 先检查是否是信贷专员
		List<HREmployeeAuditorEntity> empCeList = hREmplAuditorDao.getEmployeeByAuditorNo(userNo, "CE");
		// 不为空，则是信贷专员
		if (!CollectionUtils.isEmpty(empCeList)) {
			Set<String> allUserNoList = new HashSet<String>();
			Set<String> userNoList = new HashSet<String>();
			userNoList.add(userNo);
			for (HREmployeeAuditorEntity e : empCeList) {
				userNoList.add(e.getEmployeeNo());
			}
			allUserNoList.addAll(userNoList);
			// 查找业务交接人
			for (String cm : userNoList) {
				List<HREmployeeAuditorEntity> empBoList = hREmplAuditorDao.getEmployeeByAuditorNo(cm, "BO");
				if (!CollectionUtils.isEmpty(empBoList)) {
					for (HREmployeeAuditorEntity bo : empBoList) {
						allUserNoList.add(bo.getEmployeeNo());
					}
				}
			}
			return sysUserDao.getUserByNosWithDMS(new ArrayList<String>(allUserNoList));
		}

		// 判断是否是信贷经理
		Boolean isCreditMgr = sysUserDao.checkUserRole(userNo, "1007");
		if (isCreditMgr) {
			// 判断是否还有其他的省区据色色
			List<String> roleList = new ArrayList<String>();
			roleList.add("bldj");
			roleList.add("1008");
			roleList.add("O2O-SHJRZJ");
			Boolean hasOtherRole = sysUserDao.checkUserRole(userNo, roleList);
			if (hasOtherRole) {
				return null;
			}

			Set<String> allUserNoList = new HashSet<String>();
			Set<String> userNoList = new HashSet<String>();
			userNoList.add(userNo);
			allUserNoList.addAll(userNoList);
			// 查找业务交接人
			for (String cm : userNoList) {
				List<HREmployeeAuditorEntity> empBoList = hREmplAuditorDao.getEmployeeByAuditorNo(cm, "BO");
				if (!CollectionUtils.isEmpty(empBoList)) {
					for (HREmployeeAuditorEntity bo : empBoList) {
						allUserNoList.add(bo.getEmployeeNo());
					}
				}
			}
			return sysUserDao.getUserByNosWithDMS(new ArrayList<String>(allUserNoList));
		}
		return null;
	}

	@Override
	public ModelResult<ActOrgEntity> getUniquePlatformWithUser(String userNo) {
		User user = eaSsoDao.getUserByNo(userNo, User.class);
		String orgId = user.getOrg();
		if (StringUtils.isBlank(orgId)) {
			return new ModelResult<ActOrgEntity>().withError("未配置组织结构", "未配置组织结构");
		}
		ActOrgEntity org = sysOrgDao.getOrgByIds(user.getOrg());
		if (org == null) {
			return new ModelResult<ActOrgEntity>().withError("组织结构不存在", "组织结构不存在");
		}
		if (StringUtils.equalsIgnoreCase(org.getParentId(), "-1")) {// 总部
			return new ModelResult<ActOrgEntity>().withModel(org);
		}
		List<ActOrgEntity> actOrgList = sysOrgDao.getLeafNodeByParentId(org.getOrgId());
		if (CollectionUtils.isEmpty(actOrgList)) {
			return new ModelResult<ActOrgEntity>().withError("找不到经办平台，请联系管理员", "找不到经办平台，请联系管理员");
		}
		ActOrgEntity actOrgEntity = null;
		for (ActOrgEntity actOrg : actOrgList) {
			if (actOrg.getOrgLevel() == 4) {
				actOrgEntity = actOrg;
				break;
			}
		}
		if (actOrgEntity == null) {
			return new ModelResult<ActOrgEntity>().withError("找不到经办平台，请联系管理员", "找不到经办平台，请联系管理员");
		}
		return new ModelResult<ActOrgEntity>().withModel(actOrgEntity);
	}*/
}
